<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;

/**
 * App\Models\UserLoginLog
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property int $login_type 登录方式:1手机账号登录，2微信授权登录
 * @property string $ip 登录ip
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereLoginType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereUserId($value)
 * @mixin \Eloquent
 * @property int $login_total 登录次数
 * @property string $login_time 登陆日期
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLoginLog whereLoginTotal($value)
 */
class UserLoginLog extends Model
{

    protected $table = 'user_login_logs';

    protected $guarded = [
        'id'
    ];

    /**
     * 手机号码登录
     */
    const TYPE_PHONE = 1;

    /**
     * 微信授权方式
     */
    const TYPE_WECHAT = 2;

    const TYPE_TEXT = [
        self::TYPE_PHONE => '手机号码',
        self::TYPE_WECHAT => '微信授权',
    ];

    /**
     * Description: 获取登录类型文本
     * Author: liuju
     * DateTime: 2019-01-07 15:22
     * @param $type
     * @return mixed
     */
    public static function getTypeText($type)
    {
        return self::TYPE_TEXT[$type];
    }

    /**
     * Description:生成登录日志
     * Author: liuju
     * DateTime: 2019-01-07 15:28
     * @param $user
     * @param $type
     */
    public static function createLog(User $user, $type)
    {
        try {
            if (self::where('user_id', $user->id)->value('id')) {
                self::where('user_id', $user->id)->update([
                    'login_type' => $type,
                    'ip' => Request::getClientIp(),
                    'login_time' => date('Y-m-d H:i:s'),
                    'login_total' => DB::raw('login_total+1')
                ]);
            } else {
                self::create([
                    'user_id' => $user->id,
                    'login_type' => $type,
                    'ip' => Request::getClientIp(),
                    'login_total' => 1,
                    'login_time' => date('Y-m-d H:i:s'),
                ]);
            }

        } catch (\Exception $e) {
            Log::debug('北京时间' . date('Y-m-d H:i:s') . '用户id为' . auth('api')->user()->id . '使用ip为' . Request::getClientIp() . '进行' . self::getTypeText($type) . '登录时，由于[' . $e->getMessage() . ']日志记录异常');
        }
    }
}
